import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_email(msg, reprot_title):
    sender = 'azj38826@163.com'
    receiver = ['azj35446@163.com', 'azj38826@163.com','604410553@qq.com','1010666711@qq.com']
    smtpserver = 'smtp.163.com'
    username = 'azj38826@163.com'
    password = 'KDWRYZGQQYGTBYLA'
    mail_title = '『TMS接口自动化测试报告』'

    # 创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = sender
    message['To'] = ','.join(receiver)
    message['Subject'] = Header(mail_title, 'utf-8')

    # 邮件正文内容
    message.attach(MIMEText(msg, 'plain', 'utf-8'))

    # # 构造附件类型1（附件为TXT格式的文本）
    # att1 = MIMEText(open('text1.txt', 'rb').read(), 'base64', 'utf-8')
    # att1["Content-Type"] = 'application/octet-stream'
    # att1["Content-Disposition"] = 'attachment; filename="text1.txt"'
    # message.attach(att1)

    # # 构造附件类型2（附件为JPG格式的图片）
    # att2 = MIMEText(open('123.jpg', 'rb').read(), 'base64', 'utf-8')
    # att2["Content-Type"] = 'application/octet-stream'
    # att2["Content-Disposition"] = 'attachment; filename="123.jpg"'
    # message.attach(att2)

    # 构造附件类型3（附件为HTML格式的网页）
    report_test = r'./reports/' + reprot_title
    att3 = MIMEText(open(report_test, 'rb').read(), 'base64', 'utf-8')
    att3["Content-Type"] = 'application/octet-stream'
    att3["Content-Disposition"] = 'attachment; filename="report_test.html"'
    message.attach(att3)
    smtp = smtplib.SMTP_SSL(smtpserver, 465)
    # HELO 向服务器标识用户身份
    smtp.helo(smtpserver)
    # 服务器返回结果确认
    smtp.ehlo(smtpserver)
    # 登录邮箱服务器用户名和密码
    smtp.login(username, password)
    print("开始发送邮件")
    smtp.sendmail(sender, receiver, message.as_string())
    print("邮件发送成功！！！")
    smtp.quit()


if __name__ == '__main__':
    send_email()
